package com.renyisima.wujiansima.demo.demo2;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

public class Demo2 {
    public static void main(String[] args) throws Exception {
        Student s = new Student();
//        获取字节码对象
        Class clazz = Student.class;
//        创建对象
        Constructor constructor = clazz.getConstructor();
        Object o = constructor.newInstance();
//        获取成员变量对象
        Field name = clazz.getDeclaredField("name");
        name.setAccessible(true);
        Field age = clazz.getDeclaredField("age");
        age.setAccessible(true);
        name.set(o,"张三");
        age.set(o,23);
        name.set(s,"伍鉴");
        age.set(s,23);
        System.out.println(o);
        System.out.println(s);
    }
}
